<?php
namespace app\admin\controller;

use app\admin\model\Partition as PartitionModel;
use app\admin\model\Platform as PlatformModel;
//use my\Tree;
use Db;
use Request;

/**
 * 区服信息
 */
class Partition extends Base
{
    /*
    显示分类信息列表
     */
    public $data;
    public function index($act = "")
    {
        $where = [];
        if (Request::has('search')) {
            $where[] = ['name', 'like', '%' . trim(Request::param('search/s', '', 'strip_tags')) . '%'];
        }
        //$list = PartitionModel::alias('a')->field('a.*,b.title as typename')->join('arctype b', 'b.id = a.typeid', 'left')->order('id desc')->where($where)->paginate(20);
        $list   = PartitionModel::order('id desc')->where($where)->paginate(config('webconfig.page'), false, ['query' => Request::param()]);
        $search = Request::param('search/s', '', 'strip_tags') ? Request::param('search/s', '', 'strip_tags') : "";
        $this->assign([
            'list'   => $list,
            'page'   => $list->render(),
            'search' => $search,
        ]);
        return $this->fetch();
    }
    /*
    信息添加
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Partition');
            if (true !== $result) {
                return $this->error($result);
            }

            $post['gamedb'] = PlatformModel::dbstr($post, 'game') ? PlatformModel::dbstr($post, 'game') : "";
            $post['userdb'] = PlatformModel::dbstr($post, 'user') ? PlatformModel::dbstr($post, 'user') : "";
            $post['logdb']  = PlatformModel::dbstr($post, 'log') ? PlatformModel::dbstr($post, 'log') : "";

            $data  = new PartitionModel;
            $model = $data->allowField(true)->save($post);

            //$data = PartitionModel::create($post);
            if (!empty($model)) {
                return $this->success('添加成功', 'admin/Partition/index');
            } else {
                return $this->error('添加失败');
            }
        }
        $data = PlatformModel::where('status',1)->select();
        $numid = UID.PartitionModel::count();

        //$tree = new Tree();
        //$data = $tree->tree($data, 'title');
        $this->assign([
            'platform'=> $data,
            'numid'=>$numid,
        ]);
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\Partition.edit');
            if (true !== $result) {
                return $this->error($result);
            }

            //$data = PlatformModel::edit($id, $post);
            $post['gamedb'] = PlatformModel::dbstr($post, 'game') ? PlatformModel::dbstr($post, 'game') : "";
            $post['userdb'] = PlatformModel::dbstr($post, 'user') ? PlatformModel::dbstr($post, 'user') : "";
            $post['logdb']  = PlatformModel::dbstr($post, 'log') ? PlatformModel::dbstr($post, 'log') : "";

            $data = new PartitionModel;

            // 过滤post数组中的非数据表字段数据
            $model = $data->allowField(true)->save($post, ['id' => $id]);

            //$model = PartitionModel::update($post);
            if ($model) {
                return $this->success('修改成功', 'admin/Partition/index');
            } else {
                return $this->error('1修改失败');
            }
        }

        $info = PartitionModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}

        $data = PlatformModel::select();

        $this->assign([
            'platform' => $data,
            'info'     => $info,
            'gamedb'   => !empty($info['gamedb']) ? unserialize($info['gamedb']) : "",
            'userdb'   => !empty($info['userdb']) ? unserialize($info['userdb']) : "",
            'logdb'    => !empty($info['logdb']) ? unserialize($info['logdb']) : "",
        ]);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delall($ids = '')
    {
        $ids  = !empty($ids) ? $ids : '';
        $data = PartitionModel::destroy($ids);
        exit;
        if (!empty($data)) {
            return $this->success('删去成功', 'admin/Partition/index');
        }
        return $this->error('删去失败');
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = PartitionModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/Partition/index');
            }
        }
        return $this->error('删去失败');
    }
    /**
     * 选择分区
     * @return [type] [description]
     */
    public function selectpartition()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        //Db::name('partition')->where('id', $v)->value('sendstatus')
        $data = Db::name('user')->update(['partitionid' => $id, 'id' => session('userid')]);
        if ($data) {
            $nid['partitionid'] = $id; //分区id
            $nid['partitionname'] = PartitionModel::where('id',$id)->value('name');
            session('partitionid', $id);
            return $this->success('选择成功', '', $nid);
        } else {
            return $this->error('选择失败');
        }

    }
}
